package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {




    @Override
    public R<Page<Category>> adminCategoryPageSelect(String name,  Integer page, Integer pageSize) {
        // 构造分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        // 构造条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Category::getName, name);
        // 添加排序
        queryWrapper.orderByAsc(Category::getSort);
        // 执行查询
        page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @Override
    public R<List<Category>> adminCategoryList(String type ) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getType, type);
        List<Category> categoryList = list(lambdaQueryWrapper);
        return R.success(categoryList);
    }


    @Override
    public R<String> adminCategoryUpdate (Category category){
        boolean updateResult = updateById(category);
        if (updateResult){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


    @Override
    public  R<String> adminCategoryDelete(String id) {
        Category category = new Category();
        category.setId(id);
        boolean result = removeById(category);
        if (result) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    public R<List<Category>> clientCategoryList(){
        List<Category> list = list();
        log.info("分类集合list={}",list);
        if (list == null){
            return R.error("没有信息");
        }
        return R.success(list);
    }


}
